<template>
  <div class="image-list">
    <a-list
      :loading="loading"
      item-layout="vertical"
      :grid="{ gutter: 16, xs: 1, sm: 2, md: 2, lg: 3, xl: 3, xxl: 4 }"
      :data-source="pictureList"
    >
      <template #renderItem="{ item }">
        <a-list-item @click="router.push(`/picture/detail/${item.id}`)">
          <a-skeleton :loading="loading" active avatar>
            <a-card hoverable class="image-card">
              <template #cover>
                <div class="image-wrapper">
                  <a-image
                    :alt="item.name"
                    :src="item.thumbnailUrl ?? item.url"
                    :preview="false"
                    :placeholder="true"
                  />
                </div>
              </template>

              <template #actions v-if="showOp">
                <a-tooltip>
                  <template #title>
                    <span>以图搜图，可以根据图片搜索,快来使用我吧</span>
                  </template>
                  <a-space @click="(e) => doSearch(e, item)">
                    <SearchOutlined class="action-icon" />
                  </a-space>
                </a-tooltip>
                <a-tooltip>
                  <template #title>
                    <span>分享链接</span>
                  </template>
                  <a-space @click="(e) => doShare(e, item)">
                    <ShareAltOutlined class="action-icon" />
                  </a-space>
                </a-tooltip>

                <a-space v-if="canEdit" @click="(e) => doEdit(e, item)">
                  <EditOutlined class="action-icon" />
                </a-space>
                <a-space v-if="canDelete" @click="(e) => doDelete(e, item)">
                  <DeleteOutlined class="action-icon" />
                </a-space>
              </template>

              <a-card-meta :title="item.name">
                <template #description>
                  <a-space direction="vertical">
                    <a-tag color="green">{{ item.category }}</a-tag>
                    <a-space wrap>
                      <a-tag color="skyblue" v-for="tag in item.tags" :key="tag">{{ tag }}</a-tag>
                    </a-space>
                  </a-space>
                </template>
              </a-card-meta>
            </a-card>
          </a-skeleton>
        </a-list-item>
      </template>
    </a-list>
  </div>

  <ShareModal v-model="open" title="分享图片" :share-url="shareUrl" />
</template>
<script lang="ts" setup>
// 请求参数
import {
  DeleteOutlined,
  EditOutlined,
  SearchOutlined,
  ShareAltOutlined,
} from '@ant-design/icons-vue'
import { useRouter } from 'vue-router'
import { deletePictureUsingPost } from '@/api/pictureController.ts'
import ShareModal from '@/components/ShareModal.vue'
import { ref } from 'vue'

interface Props {
  pictureList?: API.PictureVO[]
  loading?: boolean
  onReload?: () => void
  showOp?: boolean
  showLike?: boolean
  canEdit?: boolean
  canDelete?: boolean
}

const props = defineProps<Props>()

const router = useRouter()
// 搜索操作
const doSearch = (e: MouseEvent, picture: API.Picture) => {
  e.stopPropagation()
  router.push({
    path: '/picture/search',
    query: {
      id: picture.id,
    },
  })
}
// 编辑操作
const doEdit = (e: MouseEvent, picture: API.Picture) => {
  e.stopPropagation()
  router.push({
    path: '/picture/add',
    query: {
      id: picture.id,
      spaceId: picture.spaceId,
    },
  })
  console.log(picture.spaceId)
}

// 分享操作
const open = ref<boolean>(false)
const shareUrl = ref<string>('')
const doShare = (e: MouseEvent, picture: API.Picture) => {
  e.stopPropagation()
  shareUrl.value = `${window.location.protocol}//${window.location.host}/picture/detail/${picture.id}`
  open.value = true
}

// 删除图片
const doDelete = async (e: MouseEvent, picture: API.Picture) => {
  e.stopPropagation()
  const res = await deletePictureUsingPost({ id: picture.id })
  if (res.data.code !== 0 && res.data.data) {
    return
  }
  props?.onReload()
}
</script>

<style scoped lang="scss">
.image-list {
  width: 100%;

  .image-card {
    width: 100%;
    transition: all 0.3s ease;

    &:hover {
      transform: translateY(-4px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    }
  }

  .image-wrapper {
    position: relative;
    width: 100%;
    padding-top: 75%; // 4:3 比例
    overflow: hidden;
    border-radius: 8px;

    :deep(.ant-image) {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
  }

  .action-icon {
    font-size: 20px;
    transition: all 0.3s ease;

    &:hover {
      transform: scale(1.2);
      color: #1890ff;
    }
  }

  :deep(.ant-card-meta-title) {
    font-size: 16px;
    margin-bottom: 8px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  :deep(.ant-card-actions) {
    background: #fafafa;
  }

  :deep(.ant-tag) {
    margin: 4px;
  }
}

// 响应式优化
@media screen and (max-width: 768px) {
  .image-list {
    .image-wrapper {
      padding-top: 100%; // 1:1 比例
    }

    .action-icon {
      font-size: 18px;
    }

    :deep(.ant-card-meta-title) {
      font-size: 14px;
    }
  }
}

// 平板设备优化
@media screen and (min-width: 769px) and (max-width: 1024px) {
  .image-list {
    .image-wrapper {
      padding-top: 85%; // 接近 4:3 比例
    }
  }
}

// 大屏幕优化
@media screen and (min-width: 1025px) {
  .image-list {
    .image-wrapper {
      padding-top: 75%; // 4:3 比例
    }
  }
}
</style>
